<?php
//created date 20110717
include_once 'Shared/Models/Vacancy.php';
class vacancy_SystemController extends Vi_Controller_Action
{
	public function indexAction()
	{
		$objPortal = new Models_Portal();
		$oldestUpdatedPortal = $objPortal->getPortalbyVacancyUpdatedDate();
		$oldestUpdatedPortal = current($oldestUpdatedPortal);
		$portalId = $oldestUpdatedPortal['portal_id'];
		$state =  $oldestUpdatedPortal['state'];
		$city =  $oldestUpdatedPortal['city'];
		$url = 'http://www.tenantplus.com/vacancysearchwebservice.cfc?wsdl';
 
        $client = new SoapClient($url, array(
                                             'trace'        => false, 
                                             'exceptions'   => false
                                             ));
        $newData = $client->SearchVacancy(
            '', //#check_radius_search
            '', //#check_university_search
			$state,  //'ca', //#search_state_province
			$city,   //'sacramento', //#search_city_metro
            '', //#search_zip_code
            '', //#search_distance
            '1', //#search_unit_type
            '1', //#studio
            '1', //#one_bedroom
            '1', //#two_bedrooms
            '1', //#three_bedrooms
            '', //#search_actual_vacancies
            '', //#property_type
            '0', //#price_range_from
            '100000000000000000' //#price_range_to
        );
        $objVacancy = new Models_Vacancy();
        $oldData = $objVacancy->getAll()->toArray();
        if(null != $newData) {
	        if(null != $oldData) {
		        foreach ($oldData as $item) {
		        	$objVacancy->delete($item['vacancy_id']);
		        }
	        }
			for ($i = 0; $i < $newData['RECORDCOUNT'];$i++) {
				$newDataEntry = get_object_vars($newData['RESULTS']->VacancyList->VacancyRecord[$i]);
				$newDataEntry['portal_id'] = $portalId;
			    $objVacancy->insert($newDataEntry);			
			}
        }
		$updatedData = array (
						'vacancy_updated_date' => date('Y-m-d')
		               );
		$objPortal->update($updatedData, array('portal_id=?' => $portalId));
	}
}